package com.framework.activemq.common;

import cn.hutool.json.JSONUtil;
import com.framework.activemq.dto.ActiveMqResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.ScheduledMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jms.JmsProperties;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.jms.*;

/**
 * @author lihanbo
 * @version 1.0
 * @date 2021/12/23 15:31
 */
@Component
@Slf4j
public class ActiveMqUtil {
    @Autowired
    private JmsMessagingTemplate jmsTemplate;
//    @Autowired
//    @Qualifier("queue")
//    private Queue queue;
//    @Autowired
//    @Qualifier("delayQueue")
//    private Queue delayQueue;
//    @Autowired
//    @Qualifier("topic")
//    private Topic topic;


    /**
     * 发送消息
     */
//    public void send(ActiveMqResult entity) {
//        jmsTemplate.convertAndSend(queue, JSONUtil.toJsonStr(entity));
//    }
    public void send(Queue q, ActiveMqResult entity) {
        jmsTemplate.convertAndSend(q, JSONUtil.toJsonStr(entity));
    }

    /**
     * 发送消息topic
     */
//    public void sendTopic(ActiveMqResult entity) {
//        jmsTemplate.convertAndSend(topic, JSONUtil.toJsonStr(entity));
//    }
    public void sendTopic(Topic t, ActiveMqResult entity) {
        jmsTemplate.convertAndSend(t, JSONUtil.toJsonStr(entity));
    }

    /**
     * 延时发送
     *
     * @param time 延迟时间
     */
    public void delaySend(ActiveMqResult entity, Long time) {
        execDelaySend(null, entity, time);
    }

    /**
     * 延时发送
     *
     * @param time 延迟时间
     */
    public void delaySend(Queue q, ActiveMqResult entity, Long time) {
        execDelaySend(q, entity, time);
    }

    private void execDelaySend(Queue q, ActiveMqResult entity, Long time) {
        Connection connection = null;
        Session session = null;
        MessageProducer producer = null;
        // 获取连接工厂
        ConnectionFactory connectionFactory = jmsTemplate.getConnectionFactory();
        try {
            // 获取连接
            connection = connectionFactory.createConnection();
            connection.start();
            // 获取session，true开启事务，false关闭事务
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            // 创建一个消息队列
//            producer = session.createProducer(q == null ? delayQueue : q);
            producer = session.createProducer(q);
            producer.setDeliveryMode(JmsProperties.DeliveryMode.PERSISTENT.getValue());
            ObjectMessage message = session.createObjectMessage(JSONUtil.toJsonStr(entity));
            //设置延迟时间
            message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
            // 发送消息
            producer.send(message);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (producer != null) {
                    producer.close();
                }
                if (session != null) {
                    session.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
